﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using bdnt_pets_system_Entity;
using System.Windows.Data;
using bdnt_pets_system.Resources;

namespace bdnt_pets_system.PageViews.DataManager
{
    public partial class NewShopWindow : BaseWindow
    {
        DataServiceReference.DataServiceClient dataClient;
        public NewShopWindow(Tbl_Shop cuTbl_Shop)
        {
            InitializeComponent();
            if (cuTbl_Shop.Id == 0)
                this.Title = "新建车间";
            else this.Title = "修改车间";

            dataClient = new DataServiceReference.DataServiceClient();
            CuTbl_Shop = cuTbl_Shop;
            BingData();
            InitEvnet();
        }
        public Tbl_Shop CuTbl_Shop
        {
            get { return this.DataContext as Tbl_Shop; }
            set { this.DataContext = value; }
        }

        void BingData()
        {
            tbShopName.SetBinding(TextBox.TextProperty, new Binding("ShopNo")
             {
                 Mode = BindingMode.TwoWay,
                 NotifyOnValidationError = true,
                 ValidatesOnExceptions = true
             });

            tbEnShopName.SetBinding(TextBox.TextProperty, new Binding("ShopName")
            {
                Mode = BindingMode.TwoWay,
                NotifyOnValidationError = true,
                ValidatesOnExceptions = true
            });
            tbRemark.SetBinding(TextBox.TextProperty, new Binding("Remark") { Mode = BindingMode.TwoWay });
        }

        /// <summary>
        /// 字段验证方法
        /// </summary>
        void BindgExpression()
        {
            tbShopName.GetBindingExpression(TextBox.TextProperty).UpdateSource();
            tbEnShopName.GetBindingExpression(TextBox.TextProperty).UpdateSource();
        }

        void InitEvnet()
        {
            btnOk.Click+=new RoutedEventHandler(btnOk_Click);
            btnCancel.Click += new RoutedEventHandler(btnCancel_Click);
            dataClient.SaveFailureInfoCompleted += new EventHandler<DataServiceReference.SaveFailureInfoCompletedEventArgs>(dataClient_SaveFailureInfoCompleted);
            dataClient.GetPartListByConditionCompleted += new EventHandler<DataServiceReference.GetPartListByConditionCompletedEventArgs>(dataClient_GetPartListByConditionCompleted);
            InitData();
        }

        void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        void btnOk_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                BindgExpression();
                if (vs.Errors.Count > 0)
                {
                    errorMsg.Text = MyStrings.sjtxbq;
                    return;
                }
                dataClient.SaveShopAsync(CuTbl_Shop);
                this.Close();
                App.ToPage(new ShopManagerControl());

            }
            catch (Exception)
            {

            }
        }

        void InitData()
        {
            dataClient.GetPartListByConditionAsync(null, 1000, 1);
        }

        void dataClient_GetPartListByConditionCompleted(object sender, DataServiceReference.GetPartListByConditionCompletedEventArgs e)
        {

        }

        void dataClient_SaveFailureInfoCompleted(object sender, DataServiceReference.SaveFailureInfoCompletedEventArgs e)
        {
            if (e.Result)
                this.DialogResult = true;
            else errorMsg.Text = MyStrings.bcsb; 
        }


    }
}

